/**
 * Created by user on 2015/9/8.
 */

var async = require('async');
var mysqlUtil = require('../../../common/utils/mysqlUtil');


exports.roleAllListService = function (req, callback) {

    var sql = "select * from sys_role where isValid = 1";
    mysqlUtil.query(sql, callback);
};


exports.roleListService = function (req, callback) {

    var body = req.body;
    var page = req.params.page;
    var size = Number(req.params.size);
    var index = (Number(req.params.page) - 1) * size;
    var totalPage = 0;


    var sql1 = "select count(*) as count from sys_role where isValid = 1";
    var sql2 = "select * from sys_role where isValid = 1  limit " + index +"," + size;

    if (body.roleName) {
        sql1 = sql1 + " and roleName like '%" + body.roleName + "%'";
        sql2 = "select * from sys_role where isValid = 1 and roleName like '%" + body.roleName +"%' limit " + index +"," + size;
    }


    async.series([

        function (callbackInner) {
            mysqlUtil.query(sql1, function (err, result) {

                if (err || result.length == 0) {
                    callbackInner(err, null);
                }else {
                    var count = result[0].count;
                    totalPage = Math.ceil(count/size);
                    var temp = {count: count, totalPage:totalPage};


                    if (page > totalPage) {
                        page = totalPage;
                        index = (Number(page) - 1) * size;
                        sql2 = "select * from sys_role where isValid = 1 order by parentID,roleOrder limit " + index +"," + size;
                    }

                    //logger.debug(temp);
                    callbackInner(err, temp);
                }

            });
        },

        function (callbackInner) {


            mysqlUtil.query(sql2, function (err, result) {

                var i = 0;
                async.each(result, function(temp, callbackInner2) {

                    temp.isValid = temp.isValid.readUInt8(0,[true]);

                    var sql = "select sysMenuID from sys_menuInRole where roleID = " + temp.roleID;
                    mysqlUtil.query(sql, function (err, menu) {

                        temp.menu = menu;
                        i++;
                        if (i == result.length) {
                            callbackInner(null, result);
                        }

                    }, callbackInner2);

                });

            });
        }

    ], callback);
};


exports.roleAddService = function (req, callback) {

    if (!(req.body.menuID && req.body.menuID.length)) {
        return callback("缺少menuid", null);
    }

    var body = req.body;
    body.isValid = 1;
    var menuID = body.menuID;
    var roleID = null;
    delete body.menuID;


    async.series([

        function (callbackInner) {
            var sql = "insert into sys_role set ?";
            mysqlUtil.query(sql, [body],function (err, result) {
                roleID = result.insertId;
                callbackInner(err, result);
            });
        },

        function (callbackInner) {

            async.each(menuID, function (menu, callbackInner2) {

                if (! roleID) {
                    callbackInner2();

                }else {
                    var sql = "insert into sys_menuInRole set ?";
                    mysqlUtil.query(sql, [{sysMenuID:Number(menu), roleID:roleID}], function (err, result) {

                        callbackInner2();
                    });
                }

            }, callbackInner);
        },

    ], callback);



};


exports.roleDeleteService = function (req, callback) {

    var body = req.body;
    if (!body.roleID) {
        callback({domain:"没有sys_roleID"}, null);
    }

    var roleID = body.roleID;
    logger.debug(roleID);

    var temp = "(";
    for(var i = 0, leng = body.roleID.length;i<leng;i++) {
        if (i == leng - 1) temp = temp + body.roleID[i];
        else temp = temp + body.roleID[i] +",";
    }
    temp += ")";


    async.series([

        function(callbackInner) {

            var sql = "delete from sys_role where roleID in " + temp;
            mysqlUtil.query(sql, callbackInner);
        },

        function(callbackInner) {
            var sql = "delete from sys_userInRole where roleID in " + temp;
            mysqlUtil.query(sql, callbackInner);
        },

        function(callbackInner) {
            var sql = "delete from sys_menuInRole where roleID in " + temp;
            mysqlUtil.query(sql, callbackInner);
        },

    ], callback);


};




exports.roleEditService = function (req, callback) {

    logger.debug(req.body);
    var body = req.body;

    if (body.roleID) body.roleID = Number(body.roleID);

    var roleID = body.roleID;
    var menuID = body.role.menuID;


    if (!roleID) {
       return callback("没有roleID", null);
    }

    if (!menuID) {
        var sql = "update sys_role set ? where sys_roleID = ?";
        mysqlUtil.query(sql, [body.role, body.roleID],callback);
    }else {

        delete body.role.menuID;

        async.series([

            function (callbackInner) {

                var sql = 'update sys_role set ? where roleID = ?';
                mysqlUtil.query(sql, [body.role, roleID], function (err, result) {
                    callbackInner(err, result);
                });
            },

            function (callbackInner) {

                var sql = "delete from sys_menuInRole where roleID = " + roleID;
                mysqlUtil.query(sql, callbackInner);
            },

            function (callbackInner) {

                async.each(menuID, function (menu, callbackInner2) {

                    var sql = "insert into sys_menuInRole set ?";
                    mysqlUtil.query(sql, [{sysMenuID:Number(menu), roleID:roleID}], callbackInner2);

                }, callbackInner);

            },

        ], callback);
    }



};